package com.yzyz.management.web;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JsonConfig;

import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import com.yzyz.bean.scenic.AttractionsBean;
import com.yzyz.service.AttractionsService;
import com.yzyz.utils.ValidateUtils;
import com.yzyz.utils.error.ErrorBean;
import com.yzyz.utils.error.Error;

/**
 * 
 * @author David Sun
 * @Date   2010-4-5
 *
 */
public class AttractionsController extends MultiActionController {
	private AttractionsService attractionsService;
	public void setAttractionsService(AttractionsService attractionsService) {
		this.attractionsService = attractionsService;
	}
	public void getAtts(HttpServletRequest request, HttpServletResponse response) throws IOException{
		int start = 0;
		int limit = 30;
		int scenicId = Integer.valueOf(request.getParameter("scenicId"));
		if(!ValidateUtils.isNull(request.getParameter("start"))){
			start = Integer.valueOf(request.getParameter("start"));
		}
		if(!ValidateUtils.isNull(request.getParameter("limit"))){
			limit = Integer.valueOf(request.getParameter("limit"));
		}
		List<AttractionsBean> list = this.attractionsService.paginagte(scenicId, start, limit);
		if(list == null){
			list = new ArrayList<AttractionsBean>();
		}
		JsonConfig config = new JsonConfig();
		config.setExcludes(new String[]{"date"});
		JSONArray arr = JSONArray.fromObject(list, config);
		StringBuffer sb 			= new StringBuffer();
		sb.append("{\"sum\":").append("\""+this.attractionsService.getCount(scenicId)+"\",");
		sb.append("\"data\":").append(arr.toString());
		sb.append("}");
		this.writeToClient(response, sb.toString());
	}
	/**
	 * 添加景点
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	public void add(HttpServletRequest request, HttpServletResponse response) throws IOException{
		AttractionsBean att = new AttractionsBean();
		att.setName(request.getParameter("name"));
		att.setDescription(request.getParameter("description"));
		att.setOwnerId(Integer.valueOf(request.getParameter("ownerId")));
		att.setDate(new Date());
		this.attractionsService.add(att);
		this.writeToClient(response, ErrorBean.getInstance(Error.SUCCESS, "添加成功", att.getId()).toJson());
	}
	/**
	 * 更新景点
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	public void update(HttpServletRequest request, HttpServletResponse response) throws IOException{
		AttractionsBean att = new AttractionsBean();
		att.setName(request.getParameter("name"));
		att.setDescription(request.getParameter("description"));
		att.setId(Integer.valueOf(request.getParameter("attId")));
		this.attractionsService.update(att);
		this.writeToClient(response, ErrorBean.getSuccessMsg());
	}
	/**
	 * 删除景点
	 * @param request
	 * @param response
	 * @throws IOException 
	 */
	public void delete(HttpServletRequest request, HttpServletResponse response) throws IOException{
		this.attractionsService.delete(Integer.valueOf(request.getParameter("attId")));
		this.writeToClient(response, ErrorBean.getSuccessMsg());
	}
	
	private void writeToClient(HttpServletResponse response, String contents) throws IOException {
		response.setHeader("Cache-Control", "no-cache");
		response.setHeader("Programa", "no-cache");
		response.setDateHeader("Expires", -1L);
		response.setContentType("text/html; charset=UTF-8");
		response.getWriter().write(contents);
		response.flushBuffer();
		response.getWriter().close();
	}
}
